﻿#include "TimerWidget.h"
#include <QFile>
#include <QMouseEvent>
#include <QApplication>


TimerWidget::TimerWidget(QWidget *parent)
    : QWidget(parent)
    , m_pParent(parent)
    , m_bMouseLeftButton(false)
    , m_bStart(false)
    , m_bPause(false)
    , m_pMagnifyTimerWidget(nullptr)
{
    InitUi();

    InitProperty();
}

TimerWidget::~TimerWidget()
{

}

void TimerWidget::InitUi()
{
    m_pRootLayout = new QVBoxLayout(this);
    m_pRootLayout->setContentsMargins(0,0,0,0);
    m_pRootLayout->setSpacing(0);

    m_pRootWidget = new QWidget(this);
    m_pRootWidget->setObjectName("RootWidget");
    m_pRootWidgetLayout = new QVBoxLayout(m_pRootWidget);
    m_pRootWidgetLayout->setContentsMargins(0,0,0,0);
    m_pRootWidgetLayout->setSpacing(0);

    // 顶部控件布局
    m_pTopWidget = new QWidget(m_pRootWidget);
    m_pTopWidget->setObjectName("TopWidget");
    m_pTopWidgetLayout = new QHBoxLayout(m_pTopWidget);
    m_pTopWidgetLayout->setContentsMargins(10, 0, 10, 0);
    m_pTopWidgetLayout->setSpacing(0);

    m_pTopLabel = new QLabel(m_pTopWidget);
    m_pTopLabel->setObjectName("TopLabel");
    m_pTopLabel->setText(tr("Timer"));

    QSpacerItem *item_0 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    m_pButtonGroup = new QButtonGroup(m_pTopWidget);
    m_pButtonGroup->setExclusive(true);


    m_pCountDownButton = new QPushButton(m_pTopWidget);
    m_pCountDownButton->setObjectName("CountDownButton");
    m_pCountDownButton->setText(tr("CountDown"));
    m_pCountDownButton->setToolTip(tr("CountDown"));
    m_pCountDownButton->setCheckable(true);
    m_pTheTimingButton = new QPushButton(m_pTopWidget);
    m_pTheTimingButton->setObjectName("TheTimingButton");
    m_pTheTimingButton->setText(tr("TheTiming"));
    m_pTheTimingButton->setToolTip(tr("TheTiming"));
    m_pTheTimingButton->setCheckable(true);

    m_pButtonGroup->addButton(m_pCountDownButton);
    m_pButtonGroup->addButton(m_pTheTimingButton);

    QSpacerItem *item_1 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    m_pCloseButton = new QPushButton(m_pTopWidget);
    m_pCloseButton->setObjectName("CloseButton");

    m_pTopWidgetLayout->addWidget(m_pTopLabel);
    m_pTopWidgetLayout->addItem(item_0);
    m_pTopWidgetLayout->addWidget(m_pCountDownButton);
    m_pTopWidgetLayout->addWidget(m_pTheTimingButton);
    m_pTopWidgetLayout->addItem(item_1);
    m_pTopWidgetLayout->addWidget(m_pCloseButton);

    // 功能控件stacked
    m_pStackedWidget = new QStackedWidget(m_pRootWidget);

    QWidget *m_pCountWidget = new QWidget();
    QVBoxLayout *m_pCountWidgetLayout = new QVBoxLayout(m_pCountWidget);
    m_pCountWidgetLayout->setContentsMargins(80,30,80,10);
    m_pCountWidgetLayout->setSpacing(0);

    m_pCountDownWidget = new CountDownWidget(m_pCountWidget);
    m_pCountWidgetLayout->addWidget(m_pCountDownWidget);

    m_pStackedWidget->addWidget(m_pCountWidget);

    QWidget *m_pTimingWidget = new QWidget();
    QVBoxLayout *m_pTimingWidgetLayout = new QVBoxLayout(m_pTimingWidget);
    m_pTimingWidgetLayout->setContentsMargins(100,30,100,10);
    m_pTimingWidgetLayout->setSpacing(0);

    m_pTheTimingWidget = new TheTimingWidget(m_pTimingWidget);
    m_pTimingWidgetLayout->addWidget(m_pTheTimingWidget);

    m_pStackedWidget->addWidget(m_pTimingWidget);

    // 底部控件布局
    m_pBottomWidget = new QWidget(m_pRootWidget);
    m_pBottomWidget->setObjectName("BottomWidget");
    m_pBottomWidgetLayout = new QHBoxLayout(m_pBottomWidget);
    m_pBottomWidgetLayout->setContentsMargins(0,0,0,0);
    m_pBottomWidgetLayout->setSpacing(30);
    m_pBottomWidgetLayout->setAlignment(Qt::AlignHCenter);

    //QSpacerItem *item_3 = new QSpacerItem(165, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    m_pResetButton = new QPushButton(m_pBottomWidget);
    m_pResetButton->setObjectName("ResetButton");
    m_pResetButton->setToolTip(tr("Reset"));
    m_pSwitchButton = new QPushButton(m_pBottomWidget);
    m_pSwitchButton->setObjectName("SwitchButton");
    m_pAmplificationButton = new QPushButton(m_pBottomWidget);
    m_pAmplificationButton->setObjectName("AmplificationButton");
    m_pAmplificationButton->setToolTip(tr("Amplification"));

    //QSpacerItem *item_4 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    //m_pBottomWidgetLayout->addItem(item_3);
    m_pBottomWidgetLayout->addWidget(m_pResetButton);
    m_pBottomWidgetLayout->addWidget(m_pSwitchButton);
    m_pBottomWidgetLayout->addWidget(m_pAmplificationButton);
    //m_pBottomWidgetLayout->addItem(item_4);

    m_pRootWidgetLayout->addWidget(m_pTopWidget);
    m_pRootWidgetLayout->addWidget(m_pStackedWidget);
    m_pRootWidgetLayout->addWidget(m_pBottomWidget);

    m_pRootLayout->addWidget(m_pRootWidget);
}

void TimerWidget::InitProperty()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background,Qt::transparent);
    setPalette(pal);
    setWindowOpacity(1);
    setAttribute(Qt::WA_TranslucentBackground);

    this->setMinimumSize(QSize(500, 300));

    this->setWindowIcon(QIcon(":/icon/timer.ico"));

    QFile resourceqss(":/qss/TimerWidget.qss");
    resourceqss.open(QFile::ReadOnly);
    this->setStyleSheet(resourceqss.readAll());
    resourceqss.close();

    m_sState = COUNTDOWN;

    m_pStackedWidget->setCurrentIndex(m_sState);

    SetResetButtonState("initial");
    SetSwitchButtonState("initial");
    SetAmplificationButtonState("initial");

    connect(m_pCloseButton, &QPushButton::clicked, this, [=](){
        emit closeed();
        this->close();
    });

    connect(m_pCountDownButton, &QPushButton::clicked, this, [=](){
        m_sState = COUNTDOWN;
        m_pStackedWidget->setCurrentIndex(m_sState);
        m_bStart = false;
        m_bPause = false;
        State(m_bStart, m_bPause);
        m_pCountDownWidget->on_reset();
        m_pTheTimingWidget->on_reset();
        m_pResetButton->setEnabled(false);
    });


    connect(m_pTheTimingButton, &QPushButton::clicked, this, [=](){
        m_sState = THETIMER;
        m_pStackedWidget->setCurrentIndex(m_sState);
        m_bStart = false;
        m_bPause = false;
        State(m_bStart, m_bPause);
        m_pCountDownWidget->on_reset();
        m_pTheTimingWidget->on_reset();
        m_pResetButton->setEnabled(false);
    });

    connect(m_pResetButton, &QPushButton::clicked, this, [=](){
        m_bStart = false;
        m_bPause = false;
        State(m_bStart, m_bPause);
        m_pCountDownWidget->on_reset();
        m_pTheTimingWidget->on_reset();
        m_pResetButton->setEnabled(false);
    });

    connect(m_pSwitchButton, &QPushButton::clicked, this, [=](){
        m_bStart = !m_bStart;
        State(m_bStart, m_bPause);
        m_bPause = true;
        m_pResetButton->setEnabled(true);
    });

    connect(m_pCountDownWidget, &CountDownWidget::timeout, this, [=](){
        m_bStart = false;
        m_bPause = false;
        SetResetButtonState("initial");
        SetSwitchButtonState("initial");
        SetAmplificationButtonState("initial");
        if(m_pMagnifyTimerWidget != nullptr)
        {
            m_pMagnifyTimerWidget->deleteLater();
            m_pMagnifyTimerWidget = nullptr;
        }
    });

    connect(m_pTheTimingWidget, &TheTimingWidget::timeout, this, [=](){
        m_bStart = false;
        m_bPause = false;
        SetResetButtonState("initial");
        SetSwitchButtonState("initial");
        SetAmplificationButtonState("initial");
        if(m_pMagnifyTimerWidget != nullptr)
        {
            m_pMagnifyTimerWidget->deleteLater();
            m_pMagnifyTimerWidget = nullptr;
        }
    });

    connect(m_pAmplificationButton, &QPushButton::clicked, this, [=](){
        if(m_pMagnifyTimerWidget == nullptr)
        {
            if(m_sState == COUNTDOWN)
            {
                m_pMagnifyTimerWidget = new MagnifyTimerWidget(m_pParent);
                m_pMagnifyTimerWidget->SetState(MagnifyTimerWidget::STATES::COUNTDOWN);
                m_pMagnifyTimerWidget->updatetimer(m_pCountDownWidget->GetSumTime());
                connect(m_pCountDownWidget, &CountDownWidget::updatetime, m_pMagnifyTimerWidget, &MagnifyTimerWidget::updatetimer);
            } else if(m_sState == THETIMER) {
                m_pMagnifyTimerWidget = new MagnifyTimerWidget(m_pParent);
                m_pMagnifyTimerWidget->SetState(MagnifyTimerWidget::STATES::THETIMER);
                m_pMagnifyTimerWidget->updatetimer(m_pTheTimingWidget->GetSumTime());
                connect(m_pTheTimingWidget, &TheTimingWidget::updatetime, m_pMagnifyTimerWidget, &MagnifyTimerWidget::updatetimer);
            }

            connect(m_pMagnifyTimerWidget, &MagnifyTimerWidget::closeed, this, [=](){
                if(m_pMagnifyTimerWidget != nullptr)
                {
                    m_pMagnifyTimerWidget->deleteLater();
                    m_pMagnifyTimerWidget = nullptr;
                }
            });
            m_pMagnifyTimerWidget->setModal(true);
            m_pMagnifyTimerWidget->show();
        }
    });

    m_pCountDownButton->setChecked(true);
}

void TimerWidget::SetResetButtonState(QString state)
{
    m_pResetButton->setProperty("State", state);
    m_pResetButton->setStyle(QApplication::style());
}

void TimerWidget::SetSwitchButtonState(QString state)
{
    m_pSwitchButton->setProperty("State", state);
    m_pSwitchButton->setStyle(QApplication::style());
}

void TimerWidget::SetAmplificationButtonState(QString state)
{
    if(state == "clicked")
    {
        m_pAmplificationButton->setEnabled(true);
    } else if(state == "initial") {
        m_pAmplificationButton->setEnabled(false);
    }
    m_pAmplificationButton->setProperty("State", state);
    m_pAmplificationButton->setStyle(QApplication::style());
}

void TimerWidget::State(bool state, bool pause)
{
    if(state)
    {
        SetResetButtonState("clicked");
        SetSwitchButtonState("clicked");
        SetAmplificationButtonState("clicked");
    } else {
        if(!pause)
        {
            SetResetButtonState("initial");
        }
        SetSwitchButtonState("initial");
        SetAmplificationButtonState("initial");
    }
    if(m_sState == COUNTDOWN)
    {
        m_pCountDownWidget->on_start(state, pause);
    } else if(m_sState == THETIMER)
    {
        m_pTheTimingWidget->on_start(state);
    }

}

void TimerWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        int eventx = event->pos().x();
        int eventy = event->pos().y();
        if(eventx >= m_pTopWidget->x() && eventx <= (m_pTopWidget->x() + m_pTopWidget->width()) &&
           eventy >= m_pTopWidget->y() && eventy <= (m_pTopWidget->y() + m_pTopWidget->height()))
        {
            m_bMouseLeftButton = true;
            m_pPoint = event->pos();
        }
    }
}

void TimerWidget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    m_bMouseLeftButton = false;
}

void TimerWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bMouseLeftButton)
    {
        int eventx = event->pos().x();
        int eventy = event->pos().y();

        int x = this->x() + (eventx - m_pPoint.x());
        int y = this->y() + (eventy - m_pPoint.y());

        this->move(QPoint(x, y));
    }
}
